﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Internet_Deliver_Therapy.Controllers;
using Internet_Deliver_Therapy.Models;
using Internet_Deliver_Therapy.Controllers.Patient;

namespace Internet_Deliver_Therapy.View.Index
{
    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlGender.Items.Add("Male");
                ddlGender.Items.Add("Female");                
            }
        }

        //Validate that NRIC is in waiting list
        //Encrypt password
        protected void Validate_Click(object sender, EventArgs e)
        {
            
            string nric =  tbNRIC.Text.ToString();
            string email = tbEmail.Text.ToString();
            PatientController patientController = new PatientController();
            UsersController userController = new UsersController();

            //Check if Psychiatrist Waiting List have patient's NRIC number.
            string psychiatrist = patientController.checkWaitingList(nric);

            nric = userController.checkEmail(email);

            if (!psychiatrist.Equals(""))
            {
                if (nric.Equals(""))
                {
                    Next(psychiatrist);
                }
                else
                {
                    ErrorMessage.Text = "The email address exist. Please use another " +
                                            "email address or contact your Psychiatrist.";
                }
            }
            else
            {
                ErrorMessage.Text = "Please check with your Psychiatrist that you are allowed to register.";
            }

            
        }

        public void Next(string psychiatrist)
        {
            string nric = tbNRIC.Text.ToString();
            string firstName = tbFirstName.Text.ToString();
            string lastName = tbLastName.Text.ToString();
            string race = tbRace.Text.ToString();
            string email = tbEmail.Text.ToString();
            string password = tbPassword.Text.ToString();
            string mobileNo = tbMobileNo.Text.ToString();
            string homeNo = tbHomeNo.Text.ToString();
            string gender_string = ddlGender.Text.ToString();
            string notifyVia = rbNotifyVia.SelectedItem.Value.ToString();
            bool newsletter_bool = cbNews.Checked;

            string day_string = Request.Form["daydropdown"].ToString();
            string mth_string = Request.Form["monthdropdown"].ToString();
            string year_string = Request.Form["yeardropdown"].ToString();

            int day_int = Int32.Parse(day_string);
            int mth_int = Int32.Parse(mth_string);
            int year_int = Int32.Parse(year_string);

            DateTime dob = new DateTime(year_int, mth_int, day_int);

            char gender = gender_string[0];

            char newsletter = 'T';
            if (newsletter_bool == false)
            {
                newsletter = 'F';
            }

            Users user = new Users(nric, firstName, lastName, race, email, password, psychiatrist, notifyVia, mobileNo, homeNo, gender, newsletter, dob);

            Session["User"] = user;

            Response.Redirect("ConsentForm.aspx");
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }
    }
}